package com.soft.mainsite.module.web.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.web.system.service.AttachmentService;
import com.soft.mainsite.mybatis.mapper.TSysAttachmentMapper;
import com.soft.mainsite.mybatis.model.TSysAttachment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * Description:
 *
 * @author Jin
 * @create 2017-08-08
 **/
@Service
public class AttachmentServiceImpl implements AttachmentService {

    @Resource
    private TSysAttachmentMapper attachmentMapper;
    @Value("${enable.virtual.path}")
    String enableVirtualPath;
    @Value("${windows.file.upload.path}")
    String windowsUploadPath;
    @Value("${linux.file.upload.path}")
    String linuxUploadPath;

    @Override
    public void save(TSysAttachment pojo) {
        pojo.setUserId("1");
        this.attachmentMapper.insertSelective(pojo);
    }


    @Override
    public String delete(Long[] ids) {
        if (CmsUtil.isNullOrEmpty(ids)) {
            return JsonUtil.toError("id不能为空！");
        }
        for (Long id : ids) {
            deleteAttachmentFile(id);
        }
        return JsonUtil.toSuccess("删除成功!");
    }

    @Override
    public TSysAttachment findByKey(String key) {
        TSysAttachment attachment = new TSysAttachment();
        attachment.setFileKey(key);
        return this.attachmentMapper.selectOne(attachment);
    }

    @Async
    public void deleteAttachmentFile(Long id) {
        TSysAttachment attachment = this.attachmentMapper.selectByPrimaryKey(id);
        if (!CmsUtil.isNullOrEmpty(attachment)) {
            File file = new File(attachment.getFilePath());
            file.delete();
            this.attachmentMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<TSysAttachment> findList(TSysAttachment pojo) {
        return this.attachmentMapper.select(pojo);
    }

    @Override
    public List<TSysAttachment> findAll() {
        return this.attachmentMapper.selectAll();
    }

    @Override
    public PageInfo<TSysAttachment> page(Integer pageNumber, Integer pageSize, TSysAttachment pojo) {
        PageHelper.startPage(pageNumber, pageSize);
        List<TSysAttachment> list = this.findList(pojo);

        for (TSysAttachment attachment : list) {
            String url = "true".equals(enableVirtualPath) ? (System.getProperty("os.name").toLowerCase().startsWith("win")
                    ?
                    "file:" + windowsUploadPath.replace("/", File.separator) + attachment.getFilePath()
                    :
                    "file:" + linuxUploadPath.replace("/", File.separator) + attachment.getFilePath())
                    :
                    "classpath:static";
            attachment.setUrl(url);
        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<TSysAttachment> page(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(this.findAll());
    }
}
